package com.cositas.dulces.lp.promo2012;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cositas.dulces.lp.promo2012.dbentities.Coupon;
import com.cositas.dulces.lp.promo2012.dbentities.Participant;
import com.cositas.dulces.lp.promo2012.dbentities.Question;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class AnswerQuestionAction implements Action {
	

	public void doAction(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		String key = req.getParameter("questionKey");
		String answer = req.getParameter(Question.field_answer);
		String fbid = req.getParameter(Participant.field_fbid);
		if (answer == null || answer.trim().length() == 0) {
			req.getRequestDispatcher("./ups.jsp").forward(req, resp);
			return;
		}
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		try {
			Key questionKey = KeyFactory.createKey(Question.entity_name, key);
			Entity question = datastore.get(questionKey);
			String reminder = (String)question.getProperty(Question.field_remainder);
			Integer r = Integer.valueOf(reminder);
			if (r <= 0) {
				req.getRequestDispatcher("./respuestaAgotada.jsp").forward(req, resp);
				return;
			}
			String correctanswer = (String)question.getProperty(Question.field_answer);
			if (correctanswer.equalsIgnoreCase(answer)) {
				// si esta en el listado, mando a respuesta correcta
				Collection<String> part = (Collection<String>)question.getProperty(Question.field_fbids);
				if (part == null) {
					part = new ArrayList<String>();
				}
				if (!part.contains(fbid)) {
					part.add(fbid);
					question.setProperty(Question.field_fbids, part);
					Integer rem = Integer.valueOf((String)question.getProperty(Question.field_remainder));
					question.setProperty(Question.field_remainder, String.valueOf(rem - 1));
					datastore.put(question);
					Key participantKey = KeyFactory.createKey(Participant.entity_name, fbid);
					Entity participant = datastore.get(participantKey);
					Integer points = Integer.valueOf((String)question.getProperty(Question.field_points));
					Integer account = Integer.valueOf((String)participant.getProperty(Participant.field_points));
					account = account + points;
					participant.setProperty(Participant.field_points, account.toString());
					datastore.put(participant);
					// si no esta, sumo, meto al listado y mando a respuesta correcta
					req.getRequestDispatcher("./respuestaCorrecta.jsp").forward(req, resp);
				} else {
					req.getRequestDispatcher("./ups.jsp").forward(req, resp);
				}
			} else {
				req.getRequestDispatcher("./respuestaIncorrecta.jsp").forward(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			req.getRequestDispatcher("./ups.jsp").forward(req, resp);
		}
	}
}
